﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BigLotterySyndicate.Classes;
using SyndicateBusiness.Classes;
using SyndicateDAO.DatabaseObject;

namespace BigLotterySyndicate.Secured.Payments
{
    public partial class Default : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.Visible)
                return;

            if (!this.IsUserLoggedIn)
                return;

            this.PageTitle += " | Manage funds";
            this.MasterPage.SetTopMenuTab(global::BigLotterySyndicate.Secured.Controls.TopNav.AdminNavSection.Payments);
            SetList();
        }

        private void SetList()
        {
            if (Page.IsPostBack)
                return;

            BindList();
        }

        private void BindList()
        {
            var query = this.BusinessFacade.FundsLogBL.SelectAllBySyndicateId(this.LoggedInUser.syndicateId).OrderByDescending(p=>p.dateCreated);


            this.TopPager.RecordCount = this.BottomPager.RecordCount = query.Count();
            if (this.TopPager.RecordCount > 0)
            {
                this.RepeaterList.DataSource = query.Skip(this.TopPager.StartIndex - 1).Take(this.TopPager.PageSize);
                this.RepeaterList.ItemDataBound += new RepeaterItemEventHandler(RepeaterList_ItemDataBound);
                this.RepeaterList.DataBind();

                this.MultiViewList.SetActiveView(this.ViewList);
            }
            else
            {
                this.MultiViewList.SetActiveView(this.ViewEmpty);
            }
        }

        void RepeaterList_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            fundsLog u = e.Item.DataItem as fundsLog;

            BigLotterySyndicate.Secured.Controls.PaymentListItem listItem = Utilities.FindControlRecursive(e.Item, "PaymentListItem") as BigLotterySyndicate.Secured.Controls.PaymentListItem;
            if (listItem != null)
                listItem.FundsLogEntry = u;
        }

        protected void Pager_PageIndexChanged(object sender, global::BigLotterySyndicate.Controls.PageIndexChangedEvetArgs e)
        {
            this.TopPager.PageIndex = this.BottomPager.PageIndex = e.PageIndex;

            this.BindList();
        }

        protected string PrintFundsRemaining()
        {

            double fundsUsers = (double)this.BusinessFacade.UserBL.GetAllBySyndicateId(this.LoggedInUser.syndicateId).Sum(u => u.funds);

            return String.Format("&pound;{0:0.00}", fundsUsers);

        }

        protected string PrintFundsUsed()
        {
            double fundsUsed = 0;
            int linesCount;
            foreach (draw d in this.BusinessFacade.DrawBL.GetAllBySyndicateId(this.LoggedInUser.syndicateId).Where(d => d.status != draw.drawstatus.OPEN))
            {
                linesCount = this.BusinessFacade.LineBL.SelectAllByDrawId(d.id).Count();

                if ((int) d.pricePerLine == 0)
                {
                    switch (d.type)
                    {
                        case draw.drawType.EUROMILLIONS:
                            fundsUsed += linesCount*2;
                            break;
                        default:
                            fundsUsed += linesCount;
                            break;

                    }
                }
                else
                {
                    fundsUsed += linesCount * (int)d.pricePerLine;
                }

            }

            return String.Format("&pound;{0:0.00}", fundsUsed);
        }
       
    }
}